<?php

namespace App\Http\Controllers;

use App\Interfaces\Express;
use App\OrderPay;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;


use App\Interfaces\OrderService;
use App\Interfaces\OrderGoodsService;
use App\Interfaces\CartService;


use App\Exceptions\Interfaces\OrderServiceException;
use Illuminate\Support\Collection;

class MyOrderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request,OrderService $orderService)
    {
       // dd(\Auth::user());
        $orderPay=OrderPay::where('closed',0)->where('user_id',\Auth::user()->id);
        if($request->pay_status==='0'){
            $orderPay=$orderPay->where('pay_status',0);
        };
        if ($request->ship_status){
            $orderPay=$orderPay->where('ship_status',$request->ship_status);
        };
        $orderPay=$orderPay->count('order_pay_id');
        return view('mobile.my.order',compact('orderPay'));

    }
    public function orderAjax(Request $request){

        //查询订单
        $orderPay=OrderPay::with('goods')->where('closed',0)->where('user_id',\Auth::user()->id)->orderBy('create_time','desc');
      //  dd($request->pay_status);

            if($request->pay_status==='0'){
                $orderPay=$orderPay->where('pay_status',0);
            };
            if ($request->ship_status){
                $orderPay=$orderPay->where('ship_status',$request->ship_status);
            };

            $orderPay=$orderPay->paginate(6);
       // dd($orderPay);
        return view('mobile.my.order_ajax',compact('orderPay'));
    }
    //取消订单
    public function cancel(OrderPay $order,Request $request,OrderService $orderService){

        $this->authorize('candleOrder',$order);

        if($order->paid_at){
            throw new OrderServiceException('订单已支付');
        }
        $orderService->cancelOrder($order);
        return Response()->json([
            'message'=>'订单取消成功',
            'code'=>1
        ]);

    }
    //订单支付成功页面
    public function success(OrderPay $order){
        $this->authorize('view',$order);
        return view('mobile.my.order_success',compact('order'));
    }
    public function received(OrderPay $order, Request $request,OrderService $orderService)
    {
        // 校验权限

        $this->authorize('sureOrder',$order);

        // 判断订单的发货状态是否为已发货
        $orderService->sureOrder($order);

        // 返回原页面
        return Response()->json(['message'=>'确认收货成功','code'=>1]);
    }
    public function applyRefund(OrderPay $order, Request $request,OrderService $orderService)
    {
        // 校验订单是否属于当前用户
        $this->authorize('refundOrder', $order);
        // 判断订单是否已付款
        if (!$order->paid_at) {
            throw new OrderServiceException('该订单未支付，不可退款');
        }

        // 判断订单退款状态是否正确
        if ($order->refund_status == OrderPay::REFUND_STATUS_APPLIED) {
            throw new OrderServiceException('该订单已经退款，请勿重复操作');
        }

        // 订单退款成功后处理
        $orderService->refundOrder($order);


        return Response()->json(['message'=>'退款申请中','code'=>1]);
    }

    /**
     * 物流状态
     * @param Request $request
     */
    public function express(Request $request){

        $order_id=$request->order_id;
        $order=OrderPay::find($order_id);
     //   $this->authorize('viewExpress',$order);

        //dd($order->ship_data);
        return  view('mobile.my.express',compact('order'));
    }
}
